#include <iostream>

#include <stdio.h>

#include <opengl/gl.h>
#include <opengl/glu.h>
#include <GLUT/GLUT.h>


#define WINDOW_WIDTH	400
#define WINDOW_HEIGHT	300


static float g_angle = 1.0f;

GLvoid initGLUT();
GLvoid frameRender();
GLvoid resizeGLScene( int width, int height );
GLvoid animation( int value );
GLvoid idleFunc();


int main (int argc, char* argv[]) 
{
	glutInit( &argc, argv );
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
	glutInitWindowSize( WINDOW_WIDTH, WINDOW_HEIGHT );
	glutInitWindowPosition( 100, 100 );
	
	glutCreateWindow( argv[0] );
	
	initGLUT();
	
	glutDisplayFunc( frameRender );
	glutReshapeFunc( resizeGLScene );
//	glutTimerFunc(30, animation, 0);
	glutIdleFunc(idleFunc);
	glutMainLoop();
	
    return 0;
}

GLvoid
initGLUT()
{
	glEnable( GL_DEPTH_TEST );
	glClearColor( 1.5f, 0.5f, 0.5f, 0.0f );
	glClearDepth( 1.0f );
	glDepthFunc( GL_LEQUAL );
	glShadeModel( GL_SMOOTH );
	glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}


GLvoid
frameRender()
{
	printf( "This is frameRender()\n" );
	
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	
	////////////////////////////////////////////////////////////////////////////
	glLoadIdentity();
	
	
	glTranslatef( -1.5f, 0.0f, -6.0f );
	glRotatef( g_angle, 0.0f, 1.0f, 0.0f );
	
	glBegin( GL_TRIANGLES );
		glColor3f( 1.0f, 0.0f, 0.0f );
		glVertex3f( 0.0f, 1.0f, 0.0f );
		glColor3f( 0.0f, 1.0f, 0.0f );
		glVertex3f( -1.0f, -1.0f, 0.0f );
		glColor3f( 0.0f, 0.0f, 1.0f );
		glVertex3f( 1.0f, -1.0f, 0.0f );
	glEnd();

	////////////////////////////////////////////////////////////////////////////
	glLoadIdentity();					
	glTranslatef(1.5f,0.0f,-6.0f);		
	glRotatef(g_angle,0.0f,1.0f,0.0f);	

	glColor3f(0.5f,0.5f,1.0f);			
	glBegin(GL_QUADS);					
		glVertex3f(-1.0f, 1.0f, 0.0f);	
		glVertex3f( 1.0f, 1.0f, 0.0f);	
		glVertex3f( 1.0f,-1.0f, 0.0f);	
		glVertex3f(-1.0f,-1.0f, 0.0f);	
	glEnd();						

	glutSwapBuffers();
	glutPostRedisplay();
}

GLvoid
resizeGLScene( int width, int height )
{
	printf("This is resizeGLScene()\n" );
	
	if ( height == 0 )
		height = 1;
	
	glViewport( 0, 0, width, height );
	
	glMatrixMode( GL_PROJECTION );
	
	glLoadIdentity();
	
	gluPerspective( 45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f );
	
//	glOrtho( -25.0f, 25.0f, -25.0f, 25.0f, 0.0f, 100.0f );
	
	glMatrixMode( GL_MODELVIEW );
	
	glLoadIdentity();
}

GLvoid
animation( int value )
{	
	g_angle += 0.1f;
	glutPostRedisplay();
}

GLvoid 
idleFunc()
{
	g_angle += 0.1f;
	glutPostRedisplay();
}